<template>
  <div>
    <el-dialog
      @closed="resetForm"
      :title="isAdd ? '新增文章' : '修改文章'"
      :visible="isVisible"
      @update:visible="$emit('update:isVisible', $event)"
      width="800px"
      :before-close="handleClose">

      <el-form :model="artForm" :rules="rules" ref="artForm" label-width="120px" v-model="loading">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="artForm.title"></el-input>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody">
          <quill-editor
            v-model="artForm.articleBody"
            ref="myQuillEditor"
            :options="editorOption"
          ></quill-editor>
          <!-- <el-input v-model="artForm.articleBody"></el-input> -->
        </el-form-item>
        <el-form-item label="视频地址" prop="videoURL">
          <el-input v-model="artForm.videoURL"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="$emit('update:isVisible', false)">取 消</el-button>
          <el-button type="primary" @click="onConfirm">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addArticle, updateArticle, queryArticle } from '@/api/interview'
import { quillEditor } from "vue-quill-editor"
export default {
  name: 'addEditDialog',
  props:{
    isVisible: Boolean,
    artId: {
      required: false
    },
    isAdd: Boolean
  },
  components: {
    quillEditor
  },
  data() {
    return {
      loading: false,
      artForm: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [{required: true, trigger: 'blur'}],
        articleBody: [{required: true, trigger: 'blur'}],
        videoURL: []
      },
      editorOption: {
        placeholder: "请输入...",
        modules: {
           toolbar:[
              ['bold', 'italic', 'underline', 'strike'],    //加粗，斜体，下划线，删除线
              ['blockquote', 'code-block'],     //引用，代码块

              [{ 'list': 'ordered'}, { 'list': 'bullet' }],     //列表

              ['link', 'image']    //上传图片、上传视频
            ]
         }
      }
    }
  },

  methods: {
    // 重置表单
    resetForm() {
      this.$refs.artForm.resetFields()
    },
     handleClose(done) {
      return done()
    },
    onEditorReady(editor) { }, // 准备编辑器

    onEditorBlur() { }, // 失去焦点事件

    onEditorChange() {}, // 内容改变事件,
    // 提交文章
    async onConfirm() {
      this.loading = true
      let res = ''
      if(this.isAdd){
        res = await addArticle(this.artForm)
      }else {
        res = await updateArticle(this.artId, this.artForm)
      }
      this.loading = false
      if(res) {
        this.$message({
          message: this.isAdd ? '添加成功' :'修改成功',
          type: 'success'
        })
      }else {
        this.$message.error(this.isAdd ? '添加失败' :'修改失败')
      }
      this.$emit('success')
      this.$emit('update:isVisible', false)
    },
    // 表单数据回填
    async rebackData(id) {
      const res = await queryArticle(id)
      this.artForm = res
    }
  },
  computed: {
    editor() {
      // false禁止编辑  true语序编辑
      return this.$refs.myQuillEditor.quill
    }
  },
  watch: {
    isVisible(val) {
      if(val && this.artId && !this.isAdd){
        this.rebackData(this.artId)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .ql-editor{
  height: 200px;
}
</style>
